package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Model;
import com.guoguo.entity.UserExt;
import com.guoguo.entity.vo.ModelsVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.service.ModelsService;
import com.guoguo.service.UploadService;
import com.guoguo.utils.*;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by KLSD on 2017/9/7.
 */
@Controller
@RequestMapping("/view")
public class ModelController extends BaseController {
    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ModelController.class);
    @Autowired
    private ModelsService modelService;
    @Autowired

    private UploadService uploadService;

    //查询
    @RequestMapping(value = "/modelList", method = RequestMethod.GET)
    public ModelAndView model(@RequestParam int pageNumber, @RequestParam int pageSize) {
        Map<String, Object> data = new HashedMap();
        PageBean<ModelsVO> models = modelService.selectModelsList(pageNumber, pageSize);
        data.put("modelsList", models);
        return returnView("/modelList", data);
    }

    /**
     * 跳转添加修改模特
     *
     * @param modelId
     * @return
     */
    @RequestMapping(value = "/saveModel", method = RequestMethod.GET)
    public ModelAndView Model(String modelId) {
        Map<String, Object> data = new HashMap();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        if ("".equals(modelId) || modelId == null) {
            data.put("modelId", UUIDUtils.getUUID());

        } else {
            Model models = modelService.selectByPrimaryKey(modelId);
            data.put("modelId", models.getModelId());
            data.put("modelName", models.getModelName());
            data.put("nickName", models.getNickName());
            data.put("modelIconLocal", AliOssUtils.getImageUrl(models.getModelIcon()));//, aliOssImageConfig));
            data.put("modelIcon", models.getModelIcon());
            data.put("modelCoverLocal", AliOssUtils.getImageUrl(models.getModelCover()));//, aliOssImageConfig));
            data.put("modelCover", models.getModelCover());
            data.put("modelJob", models.getModelJob());
            data.put("sex", models.getSex());
            data.put("height", models.getHeight());
            data.put("weight", models.getWeight());
            data.put("bust", models.getBust());
            data.put("waistline", models.getWaistline());
            data.put("hipline", models.getHipline());
            data.put("country", models.getCountry());
            data.put("city", models.getCity());
        }
        return returnView("/model/modelAdd", data);
    }

    /***
     * 保存添加修改
     * @param modelId
     * @param modelName
     * @param nickName
     * @param modelJob
     * @param sex
     * @param height
     * @param weight
     * @param bust
     * @param waistline
     * @param hipline
     * @param country
     * @param city
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrupdateModel", method = RequestMethod.POST)

    public JSONObject updateModel(@RequestParam String modelId, @RequestParam String modelName, @RequestParam String nickName, @RequestParam String modelIcon, @RequestParam String modelCover, @RequestParam String modelJob, @RequestParam int sex, @RequestParam String height,
                                  @RequestParam String weight, @RequestParam String bust, @RequestParam String waistline, @RequestParam String hipline, @RequestParam String country, @RequestParam String city) {

        Model model = modelService.addModel(modelId, modelName, nickName, modelIcon, modelCover, modelJob, sex, height, weight, bust, waistline, hipline, country, city, "1");

        return returnResult(200, model, "");

    }

    /**
     * 刪除模特
     *
     * @param modelId
     * @return
     */

    @RequestMapping(value = "/deleteModel", method = RequestMethod.GET)
    public ModelAndView deleteModel(String modelId) {
        int i = modelService.UDByPrimaryKey(modelId);
        Map<String, Object> data = new HashedMap();
        PageBean<ModelsVO> model = modelService.selectModelsList(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        data.put("modelsList", model);
        return returnView("/modelList", data);

    }

    /**
     * 模特头像上传
     *
     * @param fileName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadModelIcon", method = RequestMethod.POST)
    public JSONObject uploadIcon(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile fileName, @RequestParam int isZip) {

        List<String> list = FileUtils.springFileUpload(request, isZip);
        List<String> ossFile = uploadService.uploadFile(list);

        JSONObject jsonObject = new JSONObject();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");

        if (ossFile.size() > 0) {
            jsonObject.put("modelIcon", AliOssUtils.getImageUrl(ossFile.get(0)));//, aliOssImageConfig));
            jsonObject.put("modelIconLocal", ossFile.get(0));

            logger.debug("=======>>>   ossFile path:" + ossFile.get(0));
        }
        return returnResult(200, jsonObject, "");
    }

    /**
     * 模特封面上传
     *
     * @param fileName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadModelCover", method = RequestMethod.POST)
    public JSONObject uploadCover(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile fileName, @RequestParam int zip) {

        List<String> list = FileUtils.springFileUpload(request, zip);
        List<String> ossFile = uploadService.uploadFile(list);

        JSONObject jsonObject = new JSONObject();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");

        if (ossFile.size() > 0) {
            jsonObject.put("modelCover", AliOssUtils.getImageUrl(ossFile.get(0)));//, aliOssImageConfig));
            jsonObject.put("modelCoverLocal", ossFile.get(0));

            logger.debug("=======>>>   ossFile path:" + ossFile.get(0));
        }
        return returnResult(200, jsonObject, "");
    }

}
